package com.dr.service.impl;

import com.dr.Enum.ExcepEnum;
import com.dr.common.ution.DateUtil;
import com.dr.db.WeChatInfoMapper;
import com.dr.exception.ServiceExcep;
import com.dr.pojo.WeChatUserInfo;
import com.dr.service.WeChatUserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by tugou on 2017/5/13.
 */
@Service
public class WeChatUserInfoServiceImpl implements WeChatUserInfoService{

    @Autowired
    WeChatInfoMapper weChatInfoMapper;
    @Override
    public PageInfo<WeChatUserInfo> getWeChatUserInfoList(WeChatUserInfo info, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<WeChatUserInfo> infos = weChatInfoMapper.getWeChatUserInfo(info);
        PageInfo<WeChatUserInfo> pageInfo =new PageInfo<WeChatUserInfo>(infos);
        return pageInfo;
    }

    @Override
    public WeChatUserInfo getWeChatUserInfo(WeChatUserInfo userInfo) throws ServiceExcep {
        List<WeChatUserInfo> infos = weChatInfoMapper.getWeChatUserInfo(userInfo);
        if(infos == null || infos.size()==0)
        {
            throw new ServiceExcep(ExcepEnum.QUERYRESULTISEMPTY.getName(),ExcepEnum.QUERYRESULTISEMPTY.getIndex());
        }
        return infos.get(0);
    }

    @Override
    public void saveWeChatUserInfo(WeChatUserInfo userInfo) throws ServiceExcep {
        userInfo.setState(1);
        userInfo.setCreateDate(DateUtil.now());
        int result = weChatInfoMapper.insertWeChatUserInfo(userInfo);
        if (result == 0){
            throw new ServiceExcep(ExcepEnum.INSERTDATAEMPTY.getName(),ExcepEnum.INSERTDATAEMPTY.getIndex());
        }
    }
}
